<template>
  <div>
    <p v-if="newarr.length == 0">请等待</p>
    <waterfall :col="2" v-else>
      <div class="pr pbImgBox" v-for="(item, index) in newarr" :key="index">
        <router-link
          :to="{ name: 'details', params: { id: item.article.articleId } }"
        >
          <img :src="item.article.images[0].dynamicUrl" alt="加载错误"
        /></router-link>
        <div class="itemtitle">
          <span class="textOver">{{ item.article.articleTitle | uparr }}</span>
          <div class="title">
            <img :src="item.article.author.coverImage.dynamicUrl" alt="" />
            <span>{{ item.article.author.nickName | upname }}</span>
            <div class="praise">
              <span>{{ item.article.author.followCount }}</span>
            </div>
          </div>
        </div>
      </div>
    </waterfall>
  </div>
</template>

<script>
export default {
  computed: {
    itemWidth() {
      return 138 * 0.5 * (document.documentElement.clientWidth / 375); // #rem布局 计算宽度
    },
    gutterWidth() {
      return 9 * 0.5 * (document.documentElement.clientWidth / 375); //#rem布局 计算x轴方向margin(y轴方向的margin自定义在css中即可)
    },
    newarr() {
      return this.$store.state.commdata.arr;
    },
  },
  mounted() {
    this.$store.dispatch("updata");
  },
  filters: {
    uparr(val) {
      if (val.length > 20) {
        return val.substr(0, 20) + "...";
      } else {
        return val;
      }
    },
    upname(val) {
      if (val.length > 10) {
        return val.substr(0, 10);
      } else {
        return val;
      }
    },
  },
  created() {
    //在页面加载时读取sessionStorage里的状态信息
    if (sessionStorage.getItem("store")) {
      this.$store.replaceState(
        Object.assign(
          {},
          this.$store.state,
          JSON.parse(sessionStorage.getItem("store"))
        )
      );
    }

    //在页面刷新时将vuex里的信息保存到sessionStorage里
    window.addEventListener("beforeunload", () => {
      sessionStorage.setItem("store", JSON.stringify(this.$store.state));
    });
  },
  methods: {},
};
</script>

<style scoped>
img {
  width: 100%;
  margin-bottom: 0.1rem;
  border-radius: 0.05rem;
}
.itemtitle {
  padding: 0.05rem;
}
.textOver {
  font-size: 0.14rem;
  padding: 0.1rem;
  margin-top: 0.1rem;
}

.title {
  margin-top: 0.1rem;
  width: 100%;
  height: 0.3rem;
  display: flex;
}
.title img {
  width: 0.24rem;
  height: 0.24rem;
  border-radius: 50%;
}
.title span {
  display: inline-block;
  margin-left: 0.12rem;
}
.praise {
  height: 14px;
  text-align: right;
  font-size: 11px;
  color: #222;
  align-self: flex-start;
  line-height: 14px;
  position: relative;
  margin-left: 0.15rem;
}
.praise::after {
  content: "";
  width: 18px;
  height: 18px;
  position: absolute;
  left: 0.1rem;
  top: 1px;
  background: url(https://pages.c-ctrip.com/you/livestream/lvpai_detail_commentprise.png);
  background-size: 100%;
}
.praise span {
  margin-top: 0.05rem;
  margin-left: 0.3rem;
}
</style>